<?php
class Parts_model extends Model {

    function parts_model()
    {
        parent::Model();
    }

	function countAllParts()
	{
		$query = $this->db->get("parts");
		return $query->num_rows();
	}

	function countPartRFQs($part_id)
	{
		$this->db->where('part_id', $part_id);
		return $this->db->get('rfq')->num_rows();
	}
	
	function getSearchResults($postInfo)
	{
		$partNumber = $postInfo['part_number'];
		$partType = $postInfo['part_type'];
		$partDescription = $postInfo['description'];

		$this->db->like('part_number', $partNumber);
		$this->db->like('part_type', $partType);
		$this->db->like('description', $partDescription, 'both');
		$this->db->orderby('id');
		$query = $this->db->get('parts');
		if ($query->num_rows() > 0) {

			return $query;
		} else {
			return '<p>Sorry, no results returned.</p>';
		}
	}

	function getAllParts()
	{
		$this->db->orderby('part_number', 'asc');
		return $this->db->get('parts');
	}

	function getParts($offset=0, $limit=100)
	{
		$this->db->orderby('id', 'asc');
		$this->db->groupby('part_number', 'asc');
		$this->db->offset($offset);
		$this->db->limit($limit);
		return $this->db->get('parts');
	}
	function getUOM()
	{
		$this->db->orderby('name', 'asc');
		return $this->db->get('parts_uom');
	}

	function getPartType()
	{
		$this->db->orderby('name', 'asc');
		return $this->db->get('parts_type');
	}

	function getAllPartInfo($id)
    {
		$this->db->where('id', $id);
		return $this->db->get('parts')->row();
    }
	
	function getPartVendor ($id, $vendor_id)
	{
		$this->db->where('id', $id);
		$this->db->where('vendor_id', $vendor_id);
		return $this->db->get('parts');
	}

	function getPartsByType ($id, $part_type)
	{
		$this->db->where('id', $id);
		$this->db->where('part_type', $part_type);
		return $this->db->get('parts');
	}


	function getSinglePart($id)
	{
		$this->db->where('id', $id);
		return $this->db->get('parts');
	}

	function addPart($partInfo)
	{
		$this->db->insert('parts', $partInfo);
		return TRUE;
	}

	function updatePart($part_id, $partInfo)
	{
		$this->db->where('id', $partInfo['id']);
		$this->db->update('parts', $partInfo);
		return TRUE;
	}
	
	function deletePart($part_id)
	{
		// Don't allow the admin to be deleted this way
		if ($part_id === 0) {
			return FALSE;
		} else {

			/**
			 * There are 5 tables of data to delete from in order to completely
			 * clear out record of this part.
			 *
			 * Handle them 1 at a time for clarity and ease of maintenance.
			 */
	

			$this->db->where('id', $part_id);
			$this->db->delete('parts');

			return TRUE;
		}
	}

}
?>
